<?php

namespace Extend\Controller;
use Think\Controller\RpcController;


/**
 * rpcController父类
 * @modiy trainyao 2016-05-06 创建
 */
class ParentController extends RpcController {
    /**
     * 检查是否有权限调用rpc接口
     * @modify trainyao 2016-05-06 创建
     * @param  array   $data       RPC传入数据
     * @return boolean             是否有权限调用rpc接口
     */
    public function checkRPCRight($data) {
        $currentTime = time();
        if (!isset($data['time'])) {
            return false;
        }
        $data['time'] = !is_numeric($data['time']) ? strtotime($data['time']) : $data['time'];
        if ($currentTime - $data['time'] >= 60) {
            return false;
        }
        if ($data['flag'] != C('MEIZHU_RPC_FLAG')) {
            return false;
        }
        return true;
    }

    /**
     * RPC返回数据格式
     * @modify trainyao 2016-05-06  创建
     * @param  integer   $httpStatus http状态码
     * @param  integer   $status     业务状态码
     * @return array               array('httpstatus' => integer, 'status' => integer, 'data' => mixin, 'info' => string)
     */
    public function returnJson($httpStatus, $status = null, $data = null) {
        if (!is_numeric($httpStatus) && is_null($status)) {
            $httpStatus = explode(',', $httpStatus);
            $status = $httpStatus[1];
            $httpStatus = $httpStatus[0];
        }
        
        return array(
            'httpstatus' => $httpStatus,
            'status' => $status,
            'data' => is_null($data) ? array() : $data,
            'info' => \Org\Util\LogicStatus::getInfo($httpStatus, $status),
        );
    }
}
